<?php
namespace Cadss\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Document
{   
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    
    /**     
     * @ORM\Column(type="integer")    
     */
    private $dUid;
    
    /**     
     * @ORM\Column(type="string", length=255)   
     * @Assert\NotBlank
     */
    
    private $dTitel;
    
    /**     
     * @ORM\Column(type="date")      
     */
     
     private $dDatum;
    
    /**     
     * @ORM\Column(type="text", length=255)   
     * @Assert\NotBlank
     */
    
    private $dOmschrijving;

    /**
     * @ORM\ManyToMany(targetEntity="Keyword", inversedBy="documents")
     * @ORM\JoinTable("DocTref",
     *  joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")},
     *  inverseJoinColumns={@ORM\JoinColumn(name="keyword_id", referencedColumnName="id")})
     * )
     */
    private $keywords;

     /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $path;
    
    /**
     * @Assert\File(maxSize="6M")
     */
    private $file;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="document")
     * @ORM\JoinColumn(name="dUid", referencedColumnName="id")
     **/       
    private $user;

    public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/documents';
    }   

    /**
     * Get file.
     *
     * @return UploadedFile
     **/
    public function getFile()
    {
        return $this->file;
    }
    
    private $temp;

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
        $this->dDatum = new \DateTime();
        //$this->dUid = $this->getUser()->getId();
        // check if we have an old image path
        if (isset($this->path)) {
            // store the old name to delete after the update
            $this->temp = $this->path;
            $this->path = null;
        } else {
            $this->path = 'initial';
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->getFile()->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->path);     

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->getUploadRootDir().'/'.$this->temp);
            // clear the temp image path
            $this->temp = null;
        }
        $this->file = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }

    /**
     * Get Id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set dUid
     *
     * @param integer $dUid
     * @return Document
     */
    public function setDUid($dUid)
    {
        $this->dUid = $dUid;

        return $this;
    }

    /**
     * Get dUid
     *
     * @return integer 
     */
    public function getDUid()
    {
        return $this->dUid;
    }

    /**
     * Set dTitel
     *
     * @param string $dTitel
     * @return Document
     */
    public function setDTitel($dTitel)
    {
        $this->dTitel = $dTitel;

        return $this;
    }

    /**
     * Get dTitel
     *
     * @return string 
     */
    public function getDTitel()
    {
        return $this->dTitel;
    }

    /**
     * Set dDatum
     *
     * @param \DateTime $dDatum
     * @return Document
     */
    public function setDDatum($dDatum)
    {
        $this->dDatum = $dDatum;

        return $this;
    }

    /**
     * Get dDatum
     *
     * @return \DateTime 
     */
    public function getDDatum()
    {
        return $this->dDatum;
    }

    /**
     * Set dOmschrijving
     *
     * @param string $dOmschrijving
     * @return Document
     */
    public function setDOmschrijving($dOmschrijving)
    {
        $this->dOmschrijving = $dOmschrijving;

        return $this;
    }

    /**
     * Get dOmschrijving
     *
     * @return string 
     */
    public function getDOmschrijving()
    {
        return $this->dOmschrijving;
    }

    /**
     * Set path
     *
     * @param string $path
     * @return Document
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Set user
     * 
     * @param \Cadss\TestBundle\Entity\User $user
     * @return Document
     **/
    public function setUser(\Cadss\TestBundle\Entity\User $user = null){
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Cadss\TestBundle\Entity\User
     **/
    public function getUser(){
        return $this->user;
    }

    public function __construct($usr){
        $this->setUser($usr);
        $this->dUid = $usr->getId();
        $this->keywords = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add keywords
     *
     * @param \Cadss\TestBundle\Entity\Keyword $keywords
     * @return Document
     */
    public function addKeywords(\Cadss\TestBundle\Entity\Keyword $keywords)
    {
        $this->keywords[] = $keywords;

        return $this;
    }

    /**
     * Remove keywords
     *
     * @param \Cadss\TestBundle\Entity\Keyword $keywords
     */
    public function removeKeywords(\Cadss\TestBundle\Entity\Keyword $keywords)
    {
        $this->keywords->removeElement($keywords);
    }

    /**
     * Get keywords
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getKeywords()
    {
        return $this->keywords;
    }
}
